
package com.nkzy.Controller;
import cn.hutool.core.bean.BeanUtil;;
import com.nkzy.domain.dto.EmpFormDTO;
import com.nkzy.domain.pojo.Emp;
import com.nkzy.domain.pojo.Result;
import com.nkzy.domain.vo.EmpVO;
import com.nkzy.service.EmpService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RequestMapping("/emps")
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;


    @PostMapping
    @Operation(summary = "保存用户信息")
    public Result saveUser(@RequestBody EmpFormDTO empFormDTO) {

        Emp emp = BeanUtil.copyProperties(empFormDTO, Emp.class);

        empService.save(emp);

        return Result.success();
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询一个用户")
    public Result getEmpById(@PathVariable Integer id) {

        Emp emp = empService.getById(id);

        EmpVO empVO = BeanUtil.copyProperties(emp, EmpVO.class);

        return Result.success();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除一个用户")
    public Result deleteEmpById(@PathVariable Integer id) {

        empService.removeById(id);
        return Result.success();
    }

    @GetMapping
    @Operation(summary = "根据id的集合查询多个用户")
    public Result getDepts(@RequestParam("ids") List<Integer> ids) {

        List<Emp> emps = empService.listByIds(ids);

        List<EmpVO> empVO = BeanUtil.copyToList(emps, EmpVO.class);
        return Result.success();
    }

    @GetMapping("/all")
    @Operation(summary = "查询所有用户")
    public Result getAllUsers(){

        List<Emp> emps=empService.list();

        List<EmpVO> empVO=BeanUtil.copyToList(emps,EmpVO.class);
        return Result.success();
    }

    @PutMapping
    @Operation(summary = "修改用户")
    public Result updateUser(@RequestBody EmpFormDTO empFormDTO){

        Emp emp = BeanUtil.copyProperties(empFormDTO, Emp.class);

        empService.updateById(emp);

        return Result.success();
    }
}


